﻿from db_helper import db_movie_list, db_movie_info
from util_tool import safe_download_page
from bs4 import BeautifulSoup as BS

def find_header(s):
	key_begin = '<div class="body">'
	pos_begin = s.find(key_begin)
	if pos_begin == -1:
		return None
		
	pos_begin += len(key_begin) #skip '<body'
	key_begin = '<header>'
	pos_begin = s.find(key_begin, pos_begin)
	if pos_begin == -1:
		return None
		
	key_end = '</header>'
	pos_end = s.find(key_end, pos_begin + len(key_begin))
	if pos_end == -1:
		return None

	pos_end = pos_end + len(key_end)
	return (pos_begin, pos_end)

def get_introduction(s, pos_begin):
	key_begin = '<div class="content">'
	pos_begin = s.find(key_begin, pos_begin)
	if pos_begin == -1:
		return None
	pos_begin += len(key_begin)
	key_begin = '<div class="introduction">'
	pos_begin = s.find(key_begin, pos_begin)
	if pos_begin == -1:
		return None
	pos_end = s.find('<div class="content">', pos_begin)
	if pos_end == -1:
		return None
	return (pos_begin, pos_end)
	
def get_content(s, pos_begin):
	key_begin = '<div class="content">'
	pos_begin = s.find(key_begin, pos_begin)
	if pos_begin == -1:
		return None
	key_end = '<div class="clear"></div>'
	pos_end = s.find(key_end, pos_begin)
	if pos_end == -1:
		return None
	return (pos_begin, pos_end)

def get_movie_info(s):
	pos_begin, pos_end = find_header(s)
	header_s = s[pos_begin:pos_end]
	header = BS(header_s)

	movie_info = dict()
	movie_info['post_title'] = header.h2.string
	movie_info['post_date ']= header.div.find_all('span')[0].string
	movie_info['post_source'] = header.div.find_all('span')[3].a['href']
	movie_info['post_source_name'] = header.div.find_all('span')[3].a.string
	pos_begin, pos_end = get_introduction(s, pos_end)
	movie_info['post_intro'] = s[pos_begin:pos_end].decode('utf8')
	intro = BS(movie_info['post_intro'])
	topics = {}
	topics['pick_url'] = intro.div.div.a['href']
	topics['name'] = intro.div.img['title']
	topics['img'] = intro.div.img['src']
	movie_info['post_topics'] = topics
	pos_begin, pos_end = get_content(s, pos_end)
	movie_info['post_content'] = s[pos_begin:pos_end].decode('utf8')
	
	return movie_info
	
if __name__ == '__main__':
	url = 'http://www.cnbeta.com/articles/254618.htm'
	s = safe_download_page(url)
	movie_info = get_movie_info(s)
#	print movie_info['introduction'].encode('gbk')
	print movie_info['post_content'].encode('gbk')

